import java.util.*;

class Person{
    public String id;

    public Person(String id) {
        this.id = id;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Person person = (Person) o;
        return Objects.equals(id, person.id);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id);
    }
}

public class Test {
    public static void main3(String[] args) {
        String str = "abc";
        str.toUpperCase();
        System.out.println(str);
    }
    public static void main2(String[] args) {
        HashBuck2<Person,Integer> hB= new HashBuck2();
        Person person1 = new Person("1234");
        Person person2 = new Person("1234");
        //如果person1.id.hashCode() 两个打印出来的值就一样，因为String复写了hashCode()
        System.out.println(person1.hashCode());
        System.out.println(person2.hashCode());

        System.out.println(person1.equals(person2));//打印false，因为Person没重写equals方法
        hB.push(person1,1);
//        hB.push(person1,1);
        System.out.println(hB.getVal(person2));
    }
    public static void main1(String[] args) {
        HashBuck hashBuck = new HashBuck();
        hashBuck.push(1,1);
        hashBuck.push(11,1111);
        hashBuck.push(21,2121);
        hashBuck.push(65,6565);
        hashBuck.push(75,7575);
        hashBuck.push(17,1717);
        hashBuck.push(27,27);
        hashBuck.push(59,59);

        System.out.println(hashBuck.getVal(21));
        System.out.println(hashBuck.getVal(111));
    }

    public void key(String str1,String str2){
        HashSet<Character> set = new HashSet<>();
        str1=str1.toUpperCase();
        str2=str2.toUpperCase();
        for (int i=0;i<str1.length();i++){
            set.add(str1.charAt(i));
        }
        for (int i = 0; i < str2.length(); i++) {
            if (set.contains(str2.charAt(i))){
                set.remove(str2.charAt(i));
            }
        }
    }

        public static void main4(String[] args) {
            Scanner in = new Scanner(System.in);
            // 注意 hasNext 和 hasNextLine 的区别
            String str1 = in.nextLine();
            String str2 = in.nextLine();
            fun(str1,str2);
        }

        public static void fun(String str1,String str2){
            HashSet<Character> set1 = new HashSet<>();
            HashSet<Character> set2 = new HashSet<>();
            // TreeSet<Character> set = new TreeSet<>();
            int count=0;
            str1=str1.toUpperCase();
            str2=str2.toUpperCase();
            for (int i=0;i<str2.length();i++){
                set1.add(str2.charAt(i));
            }
            for (int i = 0; i < str1.length(); i++) {
                if (!set1.contains(str1.charAt(i))&&
                        !set2.contains(str1.charAt(i))){
                    set2.add(str1.charAt(i));
                    System.out.print(str1.charAt(i));
                }
            }
        }

    public static void main(String[] args) {
        String[] str = {"i","love","leetcode","i","love","coding"};
        System.out.println(topKFrequent(str, 3));
    }

    // 前K个高频单词
    public static List<String> topKFrequent(String[] words, int k) {
        HashMap<String,Integer> map = new HashMap<>();
        PriorityQueue<Map.Entry<String,Integer>> pq=new PriorityQueue<>(new Comparator<Map.Entry<String, Integer>>() {
            @Override
            public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
                if(o1.getValue().compareTo(o2.getValue())==0){
                    return o1.getKey().compareTo(o2.getKey());
                }
                return o1.getValue().compareTo(o2.getValue());
            }
        });

        int count=0;
        for(int i=0;i<words.length;i++){
            if(!map.containsKey(words[i])){
                map.put(words[i],1);
            }else{
                int num=map.get(words[i]);
                map.put(words[i],num+1);
            }
        }
        for(Map.Entry<String,Integer> entry : map.entrySet()){
            if(count<k){
                pq.offer(entry);
                count++;
            }else{
                if(pq.peek().getValue().compareTo(entry.getValue())<0){
                    pq.poll();
                    pq.offer(entry);
                }else{
                    if(pq.peek().getValue().compareTo(entry.getValue())==0&&pq.peek().getKey().compareTo(entry.getKey())>0){
                        pq.poll();
                        pq.offer(entry);
                    }
                }
            }
        }
        ArrayList<String> list = new ArrayList<>();
        for(int i=0;i<k;i++){
            list.add(pq.poll().getKey());
        }
        Collections.reverse(list);
        return list;
    }
}
